﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Road3App.Model.CrossroadNs;
using Road3App.Views.UserControls.EditControls.Crossroad;

namespace Road3App.ViewModels
{
    public class CrossroadViewModel : ViewModelBase
    {
        private CrossRoad _cr;        

        #region properties

        private double _angle;
        public double RotationAngle
        {
            get { return _angle; }
            set { _angle = value; RaisePropertyChanged("RotationAngle"); }
        }

        private ObservableCollection<CellUC> _cellCollection;
        public ObservableCollection<CellUC> CellCollection
        {
            get { return _cellCollection; }
            set { _cellCollection = value; RaisePropertyChanged("CellCollection"); }
        }

        private ObservableCollection<TrafficLightUC> _tlCollection;
        public ObservableCollection<TrafficLightUC> TrafficLightCollection
        {
            get { return _tlCollection; }
            set { _tlCollection = value; RaisePropertyChanged("TrafficLightCollection"); }
        }
        
        private int _width;
        public int width
        {
            get { return _width; }
            set { _width = value; RaisePropertyChanged("width"); }
        }

        private int _heigth;
        public int heigth
        {
            get { return _heigth; }
            set { _heigth = value; RaisePropertyChanged("height"); }
        }

        public string Param { get; set; }
        #endregion

        #region events
        public event EventHandler GridChanged;
        public void OnGridChanged()
        {
            if (this.GridChanged != null)
            {
                this.GridChanged(this, EventArgs.Empty);
            }
        }
        #endregion
        
        public CrossroadViewModel(CrossRoad cr, string param)
        {
            _cr = cr;
            Param = param;
            CellCollection = new ObservableCollection<CellUC>();
            width = _cr.width;
            heigth = _cr.height;
            RotationAngle = _cr.Angle;

            _cr.GridChanged += (s, ea) =>
                {
                    CellCollection = 
                        new ObservableCollection<CellUC>(_cr.Grid.Select(c => new CellUC(c)));
                    TrafficLightCollection = 
                        new ObservableCollection<TrafficLightUC>(_cr.Grid.Select(c => new TrafficLightUC(c)));

                    width = _cr.width;
                    heigth = _cr.height;
                    OnGridChanged();
                };

            _cr.OnGridChanged();
        }
    }
}
